/**************************************************************

This do file creates appendix figures 5 and 6;

**************************************************************/
capture log close
capture program drop _all
capture macro drop _all
estimates drop _all
set matsize 2000
drop _all
set more off

*UPDATE PATH NAMES;
global ched_data 
global output 
***************************************************************
#delimit ;

use "${ched_data}analysis_sample.dta" ;

xtset provcode year;

*SET CONTROLS;
global controls_yr emp_f_2564_90_yr* emp_m_2564_90_yr* female_90_yr* age_90_yr* nurse_cen_pc90_yr*;
global controls_yr_full emp_f_2564_90_yr* emp_m_2564_90_yr* female_90_yr* age_90_yr* nurse_cen_pc90_yr* _yrschl_2564_90_yr* urban_90_yr* ; 



*******************************************************************************;
*Appendix Figure 5;
*******************************************************************************;
*Overall;
#delimit ;
foreach var of varlist nurseonlyprog_private oldnn_newnurse_private  {;
		
		if "`var'"=="nurseonlyprog_private" {; local t="1"; };
		if "`var'"=="oldnn_newnurse_private" {; local t="2"; };

		if "`var'"=="nurseonlyprog_private" {; local w="1994"; };
		if "`var'"=="oldnn_newnurse_private" {; local w="1994"; };

		if "`var'"=="nurseonlyprog_private" {; local c="New Nursing Institutions"; };
		if "`var'"=="oldnn_newnurse_private" {; local c="Added Nursing Programs"; };
	
		if "`var'"=="nurseonlyprog_private" {; local v="ylabel(-1(1)4) ymtick(-1(.5)4)"; };
		if "`var'"=="oldnn_newnurse_private" {; local v="ylabel(-1(1)4) ymtick(-1(.5)4)"; };

		**************************;
		
		preserve;
	
		*EVENT STUDY VERSION;
		char year[omit]1999;
		xi: reg `var'  i.year|high1 i.year i.provcode $controls_yr if year>=1991&year<=2013, cluster(province) noomit;
		estimates store event`t', title('var');
		test _IyeaXhig_1992 _IyeaXhig_1993 _IyeaXhig_1994 _IyeaXhig_1995 _IyeaXhig_1996 _IyeaXhig_1997 _IyeaXhig_1998; 		
		sum `var' if high1==1 & year==2000;
		local mean=`r(mean)';
					
		*Use 1.65 for 90 percent confidence intervals;
		foreach y of numlist 1991(1)1998 2000(1)2013 {;
				gen b_`y' = _b[_IyeaXhig_`y'];
				gen upper_`y' = b_`y' + ( _se[_IyeaXhig_`y'] * 1.96 );
				gen lower_`y' = b_`y' - ( _se[_IyeaXhig_`y'] * 1.96 );
		};

			gen b_1999=0;
			
			*KEEP ONLY THE REGRESSION COEFFICIENTS AND CONFIDENCE INTERVALS;
			keep b_* upper_* lower_* ;
	
			*COLLAPSE DOWN TO ONE OBS PER REGRESSOR;
			collapse b_* upper_* lower_* ; 
			
			gen temp=1;
			reshape long b_ upper_ lower_ , i(temp) j(year); 
			drop temp;
			
			drop if year<`w';
			
			twoway (line b_ year, lcolor(black) lpattern(solid))
					(line upper_  year, lcolor(black) lpattern(dot))
					(line lower_ year, lcolor(black) lpattern(dot)),

					legend(off)
					xtitle("Year") 
					yline(0, lstyle(shortdash_dot)) ylab(, nogrid) ytitle(`c')
					plotregion(fcolor(white)) graphregion(fcolor(white))
					xline(2000 2007, lpattern(solid) lcolor(gs14)) `v';
					
			graph export "${output}Main Figures/EventStudy_`var'.pdf", replace;

			
	restore;	
};

*******************************************************************************;
*Create below median private programs in pre-period;
su nurseprog_priv if year==1999, d;
gen temp_priv99=0 if year==1999;
replace temp_priv99=1 if nurseprog_priv>=r(p50)&year==1999;
bys province: egen median_priv99=max(temp_priv99);

*Get percent for entire pre-period;
preserve;
keep if year>=1991&year<=1999;
collapse (sum) nurseprog nurseprog_priv nurseprog_pub, by(province high1);

gen priv_total=nurseprog_priv/nurseprog;
replace priv_total=0 if priv_total==.;
gen med_privtot=1 if priv_total>.938;
replace med_privtot=0 if priv_total<=.938; 
keep province med_privtot priv_total;

save "${cheddata}median_supply.dta", replace;

restore;

merge n:1 province using "${cheddata}median_supply.dta";

drop _merge;

gen totalprog=nurseprog-bothprog+nonnurseprog;
gen totalprog_priv=nurseprog_private-bothprog_private+nonnurseprog_private;

*Get percent of total programs for entire pre-period;
preserve;
keep if year>=1991&year<=1999;
collapse (sum) nurseprog nurseprog_priv nurseprog_pub bothprog bothprog_private bothprog_public nonnurseprog_public nonnurseprog_private nonnurseprog, by(province high1);

gen totalprog=nurseprog-bothprog+nonnurseprog;
gen totalprog_priv=nurseprog_priv-bothprog_priv+nonnurseprog_priv;
gen totalprog_public=nurseprog_public-bothprog_public+nonnurseprog_public;

*Calculate number of private schools with no nursing program;
gen nonnurseprogonly_priv=nonnurseprog_priv-bothprog_priv;
gen nnpriv_total=nonnurseprogonly_priv/totalprog_priv; 

*Split at median;
su nnpriv_total if high1==1, d;
su nnpriv_total if high1==0, d;

gen nnmed_priv_t=1 if nnpriv_total>.7526882&high1==1;
replace nnmed_priv_t=0 if nnpriv_total<=.7526882&high1==1; 
replace nnmed_priv_t=1 if nnpriv_total>.8683333&high1==0;
replace nnmed_priv_t=0 if nnpriv_total<=.8683333 &high1==0; 

keep province nnmed_priv_t;

save "${cheddata}totalmedian_supply.dta", replace;

restore;

merge n:1 province using "${cheddata}totalmedian_supply.dta";

drop _merge;

*******************************************************************************;
*Appendix Figure 6;
*******************************************************************************;

#delimit ;
foreach f in "nnmed_priv_t==0"	"nnmed_priv_t==1" {;

		if "`f'"=="nnmed_priv_t==0" {; local g="nnt_0"; };
		if "`f'"=="nnmed_priv_t==1" {; local g="nnt_1"; };	
		
	foreach var of varlist enroll_tot1  {;
		
		if "`var'"=="enroll_tot1" {; local t="2"; };

		if "`var'"=="enroll_tot1" {; local v="ylabel(-5(2)5) ymtick(-5(1)5)"; };

		if "`var'"=="enroll_tot1" {; local w="1991"; };

		if "`var'"=="enroll_tot1" {; local c="Percentage Points"; };
		
		preserve;

		**************************;
		*EVENT STUDY VERSION;
		char year[omit]1999;
		xi: reg `var'  i.year|high1 i.year i.provcode $controls_yr if year>=1991&year<=2013 &`f', cluster(province) noomit;
		estimates store event`t', title('var');
		test _IyeaXhig_1992 _IyeaXhig_1993 _IyeaXhig_1994 _IyeaXhig_1995 _IyeaXhig_1996 _IyeaXhig_1997 _IyeaXhig_1998; 		
		sum `var' if high1==1 & year==2000;
		local mean=`r(mean)';
					
		foreach y of numlist 1991(1)1998 2000(1)2013 {;
				gen b_`y' = _b[_IyeaXhig_`y'];
				gen upper_`y' = b_`y' + ( _se[_IyeaXhig_`y'] * 1.96 );
				gen lower_`y' = b_`y' - ( _se[_IyeaXhig_`y'] * 1.96 );
		};

			gen b_1999=0;
			
			keep b_* upper_* lower_* ;
			
			
			collapse b_* upper_* lower_* ; 
			
			gen temp=1;
			reshape long b_ upper_ lower_ , i(temp) j(year); 
			drop temp;
			
			drop if year<`w';
			
			twoway (line b_ year, lcolor(black) lpattern(solid))
					(line upper_  year, lcolor(black) lpattern(dot))
					(line lower_ year, lcolor(black) lpattern(dot)),
					legend(off)
					xtitle("Year") 
					yline(0, lstyle(shortdash_dot)) ylab(, nogrid) ytitle(`c')
					plotregion(fcolor(white)) graphregion(fcolor(white))
					xline(2000 2007, lpattern(solid) lcolor(gs14))
					`v';
					
			graph export "${output}Main Figures/supp_`g'_`var'.pdf", replace;

			
	restore;	
	};
};

